import os.path
from typing import Set, Optional, TextIO

from bald_spider.duplicate_filter import BaseFilter
from bald_spider.utils.log import get_logger


class MemoryFilter(BaseFilter):

    def __init__(self, crawler):
        self.fingerprint: Set[str] = set()
        logger = get_logger(f"{self}", crawler.settings.get('LOG_LEVEL'))
        debug: bool = crawler.settings.getbool('FILTER_DEBUG')
        super().__init__(logger, crawler.stats, debug)
        self._file: Optional[TextIO] = None
        self.set_file(crawler.settings.get('REQUEST_DIR'))

    def add(self, fp: str) -> None:
        self.fingerprint.add(fp)
        if self._file:
            self._file.write(fp + "\n")

    def __contains__(self, item):
        return item in self.fingerprint

    def set_file(self, request_dir) -> None:
        if request_dir:
            self._file = open(os.path.join(request_dir, 'request_fingerprints.txt'), 'a+')
            self._file.seek(0)
            self.fingerprint.update(fp.strip() for fp in self._file)

    async def closed(self) -> None:
        if self._file:
            self._file.close()
